//
//  HMWelcomeView.swift
//  SinaWeiBo
//
//  Created by immncjykeny on 2017/5/9.
//  Copyright © 2017年 immncjykeny. All rights reserved.
//

import UIKit
import SDWebImage

class HMWelcomeView: UIView {
    
    @IBOutlet weak var avatarImage: UIImageView!
    
    @IBOutlet weak var tipLabel: UILabel!

    @IBOutlet weak var bottomCons: NSLayoutConstraint!
    
    class func welcomeView() -> HMWelcomeView {
        let nib = UINib(nibName: "HMWelcomeView", bundle: nil)
        
        let view = nib.instantiate(withOwner: nil, options: nil)[0] as! HMWelcomeView
        //在这里设置XIB视图大小 否则默认是 600 * 600
        view.frame = UIScreen.main.bounds
        
        return view
    }
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
    }
    override func awakeFromNib() {
        guard let urlString = HMShareManager.shared.userAccount.avatar_large,
              let url = URL(string: urlString)
          else {
            return
        }
        //如果网络图片没有下载完成 先使用占位图像 如果不使用占位图像 那么网络图片会被清空
        avatarImage.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
        //设置圆角
        avatarImage.layer.cornerRadius = avatarImage.bounds.width * 0.5
        avatarImage.clipsToBounds = true
        
    }
    //当调用这个方法视图已经显示在window上面
    override func didMoveToWindow() {
        
        super.didMoveToWindow()
        
        self.layoutIfNeeded()
        
        bottomCons.constant = bounds.size.height - 200
        
       UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity:0 , options: [], animations: {
        //更新约束
            self.layoutIfNeeded()
        
        
       }) { (_) in
          UIView.animate(withDuration: 1.0, animations: {
            
            self.tipLabel.alpha = 1
            
          }, completion: { (_) in
            
            self.removeFromSuperview()
            
          })
        }
        
    }

}
